package com.kun.rental.service.impl;

import com.kun.rental.entity.CarrentalInfo;
import com.kun.rental.entity.Order;
import com.kun.rental.mapper.CarrentalInfoMapper;
import com.kun.rental.mapper.OrderMapper;
import com.kun.rental.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ikun
 * @since 2024-07-04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Autowired
    private CarrentalInfoMapper carrentalInfoMapper;


    @Autowired
    private OrderMapper orderMapper;

    /**
     * 添加订单
     * @param order
     * @return
     */
    @Override
    public boolean insert(Order order) {
        //将当前车辆的状态，由未租改成已租
        Integer carrentalId = order.getCarrentalId();
        CarrentalInfo carrentalInfo = carrentalInfoMapper.selectById(carrentalId);
        carrentalInfo.setStatus(1);
        carrentalInfoMapper.updateById(carrentalInfo);
        //生成订单编号
        String number = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(LocalDateTime.now());
        order.setOrderNum(number);
        order.setRentalTime(LocalDateTime.now());
        return orderMapper.insert(order)>0;
    }
}
